package com.sinodata.bsm.cicp.collector.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.CommandReader;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: 
 * </p>
 *
 * @author liulibo
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-20 下午3:35:40          liulibo        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class SumRegexMatchShellCollector extends Collector {

    private static final Logger logger = Logger.getLogger(SumRegexMatchShellCollector.class);

    public SumRegexMatchShellCollector() {

    }

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        String cmd = this.paramMap.get("collectCmd");
        String regex = this.paramMap.get("Regex");
        Pattern pattern = Pattern.compile(regex);
        BufferedReader reader = null;
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        try {
            reader = CommandReader.getReader(ip, cmd, (int) this.interval, 0);
            String readLine = null;
            double[] tmpValues = new double[propMetas.length];
            for (int i = 0; i < tmpValues.length; i++) {
                tmpValues[i] = 0.0d;
            }
            while ((readLine = reader.readLine()) != null) {
                Matcher matcher = pattern.matcher(readLine);
                if (matcher.find()) {
                    for (int i = 0; i < tmpValues.length; i++) {
                        tmpValues[i] += Double.parseDouble(matcher.group(i + 1));
                    }
                }
            }
            ResPropertyValue propertyValueX = new ResPropertyValue();
            ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[tmpValues.length];
            for (int i = 0; i < propValues.length; i++) {
                propValues[i] = ResPropertyValue.createPropValue(String.valueOf(tmpValues[i]), propMetas[i]);
            }
            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
        } catch (Exception e) {
            logger.error("failed to collect by SumRegexMatchShellCollector", e);
            throw new CollectorException(e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close BufferedReader", e);
                }
            }
        }
        return values;
    }

    @Override
    public String[] getParamMeta() {
        //return new String[] { "collectCmd", "Regex", "matchNum" };
        return new String[] { "collectCmd", "Regex", "matchNum" };
    }

    @Override
    public String[] getPropMeta() {
        //int count = Integer.parseInt(this.paramMap.get("matchNum"));
        String regex = this.paramMap.get("Regex");
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher("");
        int count = matcher.groupCount();
        String[] propMetas = new String[count];
        for (int i = 1; i <= propMetas.length; i++) {
            propMetas[i - 1] = "$" + i;
        }
        return propMetas;
    }
}
